package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.com.ruanmeng.demon.ForGetPswM;
import com.com.ruanmeng.demon.RegisterM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 注册设置密码
 */
public class RegisterSetPswActivity extends BaseActivity {

    @BindView(R.id.ed_psw_yzm)
    EditText edPswYzm;
    @BindView(R.id.bt_psw_next)
    Button btPswNext;
    @BindView(R.id.cb_setpsw_visible)
    CheckBox cbSetpswVisible;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_set_psw);
        ButterKnife.bind(this);
        AddLoginActivity(this);
        if (TextUtils.isEmpty(getIntent().getStringExtra("forget"))) {
            ChangLayTitle("设置登录密码");
        } else {
            ChangLayTitle("重设密码");
        }

        LayBack();
        init();
    }

    public void init() {
        edPswYzm.addTextChangedListener(this);
        btPswNext.setClickable(false);
    }

    @Override
    public void onTextChanged(CharSequence c, int i, int i1, int i2) {
        super.onTextChanged(c, i, i1, i2);
        if (c.length() > 7) {
            btPswNext.setBackgroundResource(R.drawable.ed_fullmain);
            btPswNext.setClickable(true);
        } else {
            btPswNext.setBackgroundResource(R.drawable.ed_fullgeray);
            btPswNext.setClickable(false);
        }
        //密码是否可见
        if (c.length() > 0) {
            cbSetpswVisible.setVisibility(View.VISIBLE);
        } else {
            cbSetpswVisible.setVisibility(View.GONE);
            edPswYzm.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            cbSetpswVisible.setChecked(false);
        }

    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.bt_psw_next://完成并登陆    去首页
                if (TextUtils.isEmpty(getIntent().getStringExtra("forget"))) {
                    getLoginData();
                } else {
                    getChangePswData();
                }
                break;
            case R.id.cb_setpsw_visible:
                if (cbSetpswVisible.isChecked()) {
                    edPswYzm.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    edPswYzm.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
        }
    }

    /**
     * 完成并登陆
     */
    public void getLoginData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ZhuCe, Const.POST);
        mRequest.add("mobile", getIntent().getStringExtra("phone"));
        mRequest.add("password", edPswYzm.getText().toString());
        mRequest.add("smscode", getIntent().getStringExtra("yzm"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(RegisterSetPswActivity.this, 0, mRequest,
                new CustomHttpListener(RegisterSetPswActivity.this, true, RegisterM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        RegisterM model = ((RegisterM) data);
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "username", model.getObject().getAccountName());
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "phone", model.getObject().getMobile());
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "psw", model.getObject().getPassword());
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "enable", model.getObject().getEnabled() + "");
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "salt", model.getObject().getSalt());
//                        PreferencesUtils.putInt(RegisterSetPswActivity.this, "Login", 1);//登陆标记
                        clearLoginActivity();
                        Intent in = new Intent(RegisterSetPswActivity.this, LoginActivity.class);
                        startActivity(in);
//                        Params.ToSy=1;
//                        clearLoginActivity();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(RegisterSetPswActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }

    /**
     * 重设密码完成并登陆
     */
    public void getChangePswData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ForgetPswLofgin, Const.POST);
        mRequest.add("mobile", getIntent().getStringExtra("phone"));
        mRequest.add("newpwd", edPswYzm.getText().toString());
        mRequest.add("newpwd2", edPswYzm.getText().toString());
        mRequest.add("smscode", getIntent().getStringExtra("yzm"));
        // 添加到请求队列
        CallServer.getRequestInstance().add(RegisterSetPswActivity.this, 0, mRequest,
                new CustomHttpListener(RegisterSetPswActivity.this, true, ForGetPswM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        ForGetPswM model = ((ForGetPswM) data);
//                        PreferencesUtils.putInt(RegisterSetPswActivity.this, "Login", 1);//登陆标记
//                        Params.ToSy=1;
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "phone", getIntent().getStringExtra("phone"));
                        PreferencesUtils.putString(RegisterSetPswActivity.this, "psw", edPswYzm.getText().toString());
                        clearLoginActivity();
                        Intent in = new Intent(RegisterSetPswActivity.this, LoginActivity.class);
                        startActivity(in);

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(RegisterSetPswActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }
}
